package com.bj58.ecdata.dashboard.databll;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.TreeMap;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.constant.ResumeConst;
import com.bj58.ecdata.dashboard.dbentity.ResumeDown;
import com.bj58.ecdata.dashboard.dbentity.ResumeEntBasic;
import com.bj58.ecdata.dashboard.dbentity.ResumeEntTotal;
import com.bj58.ecdata.dashboard.dbentity.ResumeEntWord;
import com.bj58.ecdata.dashboard.dbentity.ResumeIncome;
import com.bj58.ecdata.dashboard.dbentity.ResumeIndBasic;
import com.bj58.ecdata.dashboard.dbentity.ResumeIndTotal;
import com.bj58.ecdata.dashboard.dbentity.ResumeQuality;
import com.bj58.ecdata.dashboard.dbentity.ResumeQuantity;
import com.bj58.ecdata.dashboard.dbentity.ResumeReadDown;
import com.bj58.ecdata.dashboard.dbentity.ResumeTalentInflow;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.DDateUtils;
import com.bj58.ecdata.dashboard.utils.ReflectUtil;
import com.bj58.ecdata.dashboard.utils.VelocityTools;
import com.google.common.base.Joiner;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class ResumeBLL {

	private static AbstractBaseService<ResumeReadDown> resumeService =ServiceBeanFactory.get(ResumeReadDown.class,null);
	private static AbstractBaseService<ResumeQuality> resumeQualityService =ServiceBeanFactory.get(ResumeQuality.class,null);
	private static AbstractBaseService<ResumeIndBasic> resumeIndBasicService =ServiceBeanFactory.get(ResumeIndBasic.class,null);
	private static AbstractBaseService<ResumeEntBasic> resumeEntBasicService =ServiceBeanFactory.get(ResumeEntBasic.class,null);
	
	private static AbstractBaseService<ResumeIndTotal> resumeIndTotalService =ServiceBeanFactory.get(ResumeIndTotal.class,null);
	private static AbstractBaseService<ResumeEntTotal> resumeEntTotalService =ServiceBeanFactory.get(ResumeEntTotal.class,null);
	private static AbstractBaseService<ResumeTalentInflow> resumeTalentInflowService =ServiceBeanFactory.get(ResumeTalentInflow.class,null);
	
	private static AbstractBaseService<ResumeQuantity> resumeQuantityService =ServiceBeanFactory.get(ResumeQuantity.class,null);	
	private static AbstractBaseService<ResumeIncome> resumeIncomeService =ServiceBeanFactory.get(ResumeIncome.class,null);
	private static AbstractBaseService<ResumeEntWord> resumeEntWordService =ServiceBeanFactory.get(ResumeEntWord.class,null);
	
	/**
	 * 查询求职者整体数据    并计算日环比 周同比 月同比
	 * @author guoxue 2015-08-19
	 */
	public static List<List<Object>> getGeneral(String date) {
		if (date == null) {
			return Collections.emptyList();
		}
		String lastDay = DDateUtils.varDate(date, "yyyy-MM-dd", Calendar.DATE, -1);// 计算日环比
		String lastWeek = DDateUtils.varDate(date, "yyyy-MM-dd", Calendar.DATE, -7);// 计算日环比
		String lastMonth = DDateUtils.varDate(date, "yyyy-MM-dd", Calendar.DATE, -28);// 计算月同比 , 按28天算，刚好4周

		final String PATTEN_GENERAL = "(stat_date = '%s' OR stat_date='%s' OR stat_date='%s' OR stat_date='%s') AND (cate_name='求职信息' OR cate_name='找兼职') group by stat_date";
		final String select = "stat_date,SUM(uv) as uv ,SUM(user_valid_add_count) as user_valid_add_count,SUM(resume_valid_add_count) as resume_valid_add_count," +
				"SUM(user_act_deliver) as user_act_deliver,SUM(resume_act_deliver) as resume_act_deliver,SUM(count_read) as count_read,SUM(count_down) as count_down";
		
		String condition = String.format(PATTEN_GENERAL, date, lastDay, lastWeek, lastMonth);
		List<ResumeIndBasic> list = resumeIndBasicService.getListByConditon(select, condition, null);
		TreeMap<String, List<ResumeIndBasic>> map = ReflectUtil.groupByField(list,"stat_date");
		
		String[] cols = {"uv","user_valid_add_count","resume_valid_add_count","user_act_deliver","resume_act_deliver","count_read","count_down"};

		String[] dates = {date, lastDay, lastWeek, lastMonth}; 
		
		List<List<Object>> finalData = Lists.newArrayList();    //最终结果
		List<ResumeIndBasic> todayData = map.get(date);         //当天的结果  计算环比和同比用
		
		//遍历查询结果，处理数据
		for(String d : dates){
					
			List<ResumeIndBasic> curData = map.get(d);			
			//如果lastDay, lastWeek, lastMonth 有哪天数据为null,就还赋值为date的数据
			if(curData==null)
			{
				curData = map.get(date);
			}
			
            List<Object> tmpList = Lists.newArrayList();
			
            ResumeIndBasic vo = curData.get(0);
            ResumeIndBasic todayVo = todayData.get(0);
         	
			for(String c : cols){
				Object val = ReflectUtil.getVal(vo, c);
				if(StringUtils.equals(date, d)){
					tmpList.add(val);
				}else{
					Object todayVal = ReflectUtil.getVal(todayVo, c);
					tmpList.add(VelocityTools.compRatio(todayVal, val));
				}
			}
			
			//获得一行结果
			finalData.add(tmpList);
		}

		return finalData;
	}
	
	/**
	 * 查询企业整体数据    并计算日环比 周同比 月同比
	 * @author guoxue 2015-08-19
	 */
	public static List<List<Object>> getGeneral2(String date) {
		if (date == null) {
			return Collections.emptyList();
		}
		String lastDay = DDateUtils.varDate(date, "yyyy-MM-dd", Calendar.DATE, -1);// 计算日环比
		String lastWeek = DDateUtils.varDate(date, "yyyy-MM-dd", Calendar.DATE, -7);// 计算日环比
		String lastMonth = DDateUtils.varDate(date, "yyyy-MM-dd", Calendar.DATE, -28);// 计算月同比 , 按28天算，刚好4周

		final String PATTEN_GENERAL = "(stat_date = '%s' OR stat_date='%s' OR stat_date='%s' OR stat_date='%s') AND (cate_name='全职招聘' OR cate_name='兼职招聘') group by stat_date";
		final String select = "stat_date,SUM(uv) as uv ,SUM(add_position) as add_position,SUM(add_position_ent) as add_position_ent," +
				"SUM(call_count) as call_count,SUM(get_zddelivery) as get_zddelivery,SUM(get_zddelivery_ent) as get_zddelivery_ent";
		
		String condition = String.format(PATTEN_GENERAL, date, lastDay, lastWeek, lastMonth);
		List<ResumeEntBasic> list = resumeEntBasicService.getListByConditon(select, condition, null);
		TreeMap<String, List<ResumeEntBasic>> map = ReflectUtil.groupByField(list,"stat_date");
		
		String[] cols = {"uv","add_position","add_position_ent","call_count","get_zddelivery","get_zddelivery_ent"};

		String[] dates = {date, lastDay, lastWeek, lastMonth};
	
		List<List<Object>> finalData = Lists.newArrayList(); //最终结果
		List<ResumeEntBasic> todayData = map.get(date);  //当天的结果  计算环比和同比用
		
		//遍历查询结果，处理数据
		for(String d : dates){
			List<ResumeEntBasic> curData = map.get(d);
			
			//如果lastDay, lastWeek, lastMonth 有哪天数据为null,就还赋值为date的数据
			if(curData==null)
			{
				curData = map.get(date);
			}
            List<Object> tmpList = Lists.newArrayList();
			
            ResumeEntBasic vo = curData.get(0);
            ResumeEntBasic todayVo = todayData.get(0);
         	
			for(String c : cols){
				Object val = ReflectUtil.getVal(vo, c);
				if(StringUtils.equals(date, d)){
					tmpList.add(val);
				}else{
					Object todayVal = ReflectUtil.getVal(todayVo, c);
					tmpList.add(VelocityTools.compRatio(todayVal, val));
				}
			}
			
			//获得一行结果
			finalData.add(tmpList);
		}

		return finalData;
	}
	
	/**
	 /**
	 * 简历整体数据--饼图
	 * @param cate 
	 * @param city 
	 * @param index 
	 * @return
	 */
	public static String getResumeTotalJson(String cate, String city, String index){

		if (cate == null||city == null||index == null) {
			return Collections.emptyList().toString();
		}
		
		List<ResumeIndTotal> list=Lists.newArrayList();
		//处理类别为总体		
		if(cate.equals("all"))		
		{		
			String select = "month_num,SUM(PC_resume) as PC_resume, SUM(M_resume) as M_resume, SUM(APP_resume) as APP_resume,SUM(PCW_resume) as PCW_resume," +
				" SUM(MW_resume) as MW_resume, SUM(APPW_resume) as APPW_resume, SUM(PC_user) as PC_user, SUM(M_user) as M_user, SUM(APP_user) as APP_user,SUM(PCW_user) as PCW_user," +
				" SUM(MW_user) as MW_user, SUM(APPW_user) as APPW_user";
			
			String c ="(cate_name='求职信息' OR cate_name='找兼职') AND city_name='%s' group by month_num";
			String condition = String.format(c, city);
			list = resumeIndTotalService.getListByConditon(select, condition, null);		
		}
		else  
		{
			list = resumeIndTotalService.getListByConditon(ResumeConst.getPieCols(index), ResumeConst.getPieCon(cate, city), null);
		}
			
		TreeMap<String, List<ResumeIndTotal>> stat_dateMap = new TreeMap<String, List<ResumeIndTotal>>(ReflectUtil.groupByField(list,"month_num"));
		
		String[] Array={};
		if(index.equals("resume_add_count"))
			Array=ResumeConst.resume_Array;
		else 
			Array=ResumeConst.user_Array;
		
		List<Object> LegendArray = Lists.newArrayList();
		for (Object  c : Array) {		
			LegendArray.add(ResumeConst.getName(c.toString()));
		}
		// 返回总对象
		JSONObject json = new JSONObject();			
		List<Object> dateList = Lists.newArrayList();
		
		JSONObject json2 = new JSONObject();			
		for (String date : stat_dateMap.keySet()) {
			
			dateList.add(date);
			List<ResumeIndTotal> dayList = stat_dateMap.get(date);
			ResumeIndTotal vo = dayList.get(0);
			
			List<Object> curList = Lists.newArrayList();			
			for (Object  c : Array) {
		
				Object name = ResumeConst.getName(c.toString());
			    Object value = ReflectUtil.getVal(vo,c.toString());

				JSONObject json3 = new JSONObject();
				json3.put("name",name);
				json3.put("value",value);
			   
				curList.add(json3);
				json2.put(date, curList);			
			}
		}	
		
		json.put("options", json2);
		
		//timeline
		json.put("timeline", dateList);
		json.put("cate", LegendArray);

		return json.toString();
	}	
	
	/**
	 * 简历整体数据--折线图
	 * @param city 
	 * @param cate
	 * @param index
	 * @return
	 */
	public static String getResumeTotalLineJson(String cate, String city, String index){
	
		if (cate == null||city == null||index == null) {
			return Collections.emptyList().toString();
		}
		
		List<ResumeIndTotal> list=Lists.newArrayList();
		//处理类别为总体		
		if(cate.equals("all"))		
		{		
			String select = "stat_date,SUM(PC_resume) as PC_resume, SUM(M_resume) as M_resume, SUM(APP_resume) as APP_resume,SUM(PCW_resume) as PCW_resume," +
				" SUM(MW_resume) as MW_resume, SUM(APPW_resume) as APPW_resume, SUM(PC_user) as PC_user, SUM(M_user) as M_user, SUM(APP_user) as APP_user,SUM(PCW_user) as PCW_user," +
				" SUM(MW_user) as MW_user, SUM(APPW_user) as APPW_user";
			
			String c ="(cate_name='求职信息' OR cate_name='找兼职') AND city_name='%s' group by stat_date";
			String condition = String.format(c, city);
			list = resumeIndTotalService.getListByConditon(select, condition, null);		
		}
		else  
		{
			list = resumeIndTotalService.getListByConditon(ResumeConst.getLineCols(index), ResumeConst.getLineCon(cate, city), null);
		}
		
		TreeMap<String, List<ResumeIndTotal>> stat_dateMap = new TreeMap<String, List<ResumeIndTotal>>(ReflectUtil.groupByField(list,"stat_date"));

		String[] Array={};
		if(index.equals("resume_add_count"))
			Array=ResumeConst.resume_Array;
		else 
			Array=ResumeConst.user_Array;
		List<Object> LegendArray =Lists.newArrayList();
		for(String c:Array)
		{
			LegendArray.add(ResumeConst.getName(c));
		}
		
		Multimap<String, Object> dataMap= ArrayListMultimap.create();
		for (String date : stat_dateMap.keySet()) {
			List<ResumeIndTotal> dayList = stat_dateMap.get(date);
			ResumeIndTotal vo = dayList.get(0);
			for (String  c : Array) {
		
			    Object value = ReflectUtil.getVal(vo,c);
		
				dataMap.put(ResumeConst.getName(c), value);			
			}
		}	

 		JSONObject ob = new JSONObject();
		ob.put("time", stat_dateMap.keySet());
		ob.putAll(dataMap.asMap());
		ob.put("cate", LegendArray);

		return ob.toString();
			
	}	
	
	
	/**
	 * 职位整体数据--饼图
	 * @param city 
	 * @param cate
	 * @param index
	 * @return
	 */
	public static String getJobTotalJson(String cate, String city, String index){

		if (cate == null||city == null||index == null) {
			return Collections.emptyList().toString();
		}
		
		List<ResumeEntTotal> list=Lists.newArrayList();
		//处理类别为总体		
		if(cate.equals("all"))		
		{		
			String select = "month_num,platform,SUM(add_position) as add_position,SUM(add_position_ent) as add_position_ent";
	
			String c ="(cate_name='全职招聘' OR cate_name='兼职招聘') AND city_name='%s' group by month_num,platform";
			String condition = String.format(c, city);
			list = resumeEntTotalService.getListByConditon(select, condition, null);		
		}
		else  
		{
			list = resumeEntTotalService.getListByConditon(ResumeConst.getJobPieCols(), ResumeConst.getJobPieCon(cate, city), null);
		}
				
		TreeMap<String, List<ResumeEntTotal>> stat_dateMap = new TreeMap<String, List<ResumeEntTotal>>(ReflectUtil.groupByField(list,"month_num"));

		String[] Array={};
		
		Array=ResumeConst.platform_Array;
			
		// 返回总对象
		JSONObject json = new JSONObject();			
		List<Object> dateList = Lists.newArrayList();
		JSONObject json2 = new JSONObject();
		for (String date : stat_dateMap.keySet()) {
			
			dateList.add(date);
			List<ResumeEntTotal> dayList = stat_dateMap.get(date);
			List<Object> curList = Lists.newArrayList();		
			for (Object  c : Array) {
		
				ResumeEntTotal vo = ReflectUtil.getByColVal(ResumeEntTotal.class, dayList, "platform", c);				
				Object name = c;
			    Object value = ReflectUtil.getVal(vo,index);

				JSONObject json3 = new JSONObject();
				json3.put("name",name);
				json3.put("value",value);
				
				curList.add(json3);
				json2.put(date, curList);
			}
		}	
		
		json.put("options", json2);
		
		//timeline
		json.put("timeline", dateList);
		json.put("cate", Array);
		
		return json.toString();
	}	
	
	/**
	 * 职位整体数据--折线图
	 * @param city 
	 * @param cate
	 * @param index
	 * @return
	 */
	public static String getJobTotalLineJson(String cate, String city, String index){
	
		if (cate == null||city == null||index == null) {
			return Collections.emptyList().toString();
		}
		
		List<ResumeEntTotal> list=Lists.newArrayList();
		//处理类别为总体		
		if(cate.equals("all"))		
		{		
			String select =  "stat_date, platform, SUM(add_position) as add_position,SUM(add_position_ent) as add_position_ent";
			
			String c ="(cate_name='全职招聘' OR cate_name='兼职招聘') AND city_name='%s' group by stat_date,platform";
			String condition = String.format(c, city);
			list = resumeEntTotalService.getListByConditon(select, condition, null);		
		}
		else  
		{
			list = resumeEntTotalService.getListByConditon(ResumeConst.getJobLineCols(), ResumeConst.getJobLineCon(cate, city), null);
		}
				
		TreeMap<String, List<ResumeEntTotal>> stat_dateMap = new TreeMap<String, List<ResumeEntTotal>>(ReflectUtil.groupByField(list,"stat_date"));
		
	    String[] Array={};
		
		Array=ResumeConst.platform_Array;
		
		Multimap<String, Object> dataMap= ArrayListMultimap.create();
		for (String date : stat_dateMap.keySet()) {
			List<ResumeEntTotal> dayList = stat_dateMap.get(date);

			for (Object  c : Array) {
		
				ResumeEntTotal vo = ReflectUtil.getByColVal(ResumeEntTotal.class, dayList, "platform", c);
				
			    Object value = ReflectUtil.getVal(vo,index);	
				dataMap.put(c.toString(), value);			
			}
		}	

 		JSONObject ob = new JSONObject();
		ob.put("time", stat_dateMap.keySet());
		ob.putAll(dataMap.asMap());
		ob.put("cate", Array);

		return ob.toString();
			
	}	
	
	/**
	 * 简历下载专题--三个饼图
	 * @param city 
	 * @param cate
	 * @param index
	 * @return
	 */
	public static String getDownSubjectJson(String cate, String city,String index){

		if (cate == null||city == null||index == null) {
			return Collections.emptyList().toString();
		}
	
		HashMap<String, String> transferMap = Maps.newHashMap();
	
		transferMap.put("zhiding_resume", "置顶简历");
		transferMap.put("pc_resume","PC端发布简历");
		transferMap.put("m_resume","移动端发布简历");
		transferMap.put("other_resume", "其他简历");		
	
		// 返回总对象
		JSONObject json = new JSONObject();
				
		//timeline
		String[] time={"201501","201502","201503","201504","201505"};

		json.put("timeline", time);
		
		JSONObject json2 = new JSONObject();
//		List<Object> dataList = Lists.newArrayList();
		

		String[] cols = {"zhiding_resume","pc_resume","m_resume","other_resume"};
		String[] cols1 = {"置顶简历","PC端发布简历","移动端发布简历","其他简历"};

		Random random = new Random();
		for(int i=0;i<5;i++){
		
			ResumeDown vo = new ResumeDown((500 + random.nextInt(500)),(100 + random.nextInt(500)),(300 + random.nextInt(500)),(400 + random.nextInt(500)));
			
			List<Object> curList = Lists.newArrayList();			
			for(String c : cols){
				
				Object val = ReflectUtil.getVal(vo, c);				
				JSONObject json3 = new JSONObject();				
				json3.put("name",transferMap.get(c));
				json3.put("value",val);
				
				curList.add(json3);
				json2.put(time[i], curList);		
			}
		}
		
		json.put("options", json2);
		json.put("cate", cols1);
			
 		return json.toString();
				
	}	
	
	
	/**
	 * 简历销售收入--饼图
	 * 
	 * @param name_type
	 * @return
	 */
	public static String getResumeIncomeJson(String name_type){

		if (name_type == null) {
			return Collections.emptyList().toString();
		}
	
		List<ResumeIncome> list=Lists.newArrayList();
					
		String select = "week_begin,cash_income,user_type";	
		String con ="stat_type='week' AND flag_type='%s' group by week_begin,user_type";
		String condition = String.format(con, name_type);
		list = resumeIncomeService.getListByConditon(select, condition, null);					
				
		TreeMap<String, List<ResumeIncome>> stat_dateMap = new TreeMap<String, List<ResumeIncome>>(ReflectUtil.groupByField(list,"week_begin"));
			
		List<Object> Array = ResumeConst.resumeIncomeMap.get(name_type);		
 //		String[] Array={"会员","非会员"};	
	
		// 返回总对象
		JSONObject json = new JSONObject();				
		//timeline
		List<Object> dateList = Lists.newArrayList();
		
		JSONObject json2 = new JSONObject();		
		for (String date : stat_dateMap.keySet()) {
			
			dateList.add(date);
			List<ResumeIncome> dayList = stat_dateMap.get(date);
			List<Object> curList = Lists.newArrayList();
			for (Object  c : Array) {
		
				ResumeIncome vo = ReflectUtil.getByColVal(ResumeIncome.class, dayList, "user_type", c);				
				Object name = c;
				Object value = ReflectUtil.getVal(vo,"cash_income");

				JSONObject json3 = new JSONObject();
				json3.put("name",name);
				json3.put("value",value);
				
				curList.add(json3);
				json2.put(date, curList);	
			}
		}	
		
		json.put("dataList", json2);
		//timeline
		json.put("timeline", dateList);
		json.put("cate", Array);
			
 		return json.toString();
				
	}	
	
	/**
	 * 简历销售收入--柱状图
	 * 
	 * @param name_type
	 * @param index
	 * @return
	 */
	public static String getResumeIncomebarJson(String index,String name_type){
	
		if (index == null||name_type == null) {
			return Collections.emptyList().toString();
		}

		List<ResumeIncome> list=Lists.newArrayList();
			
		String select =  "stat_date, user_type, sum(sum_income) as sum_income,sum(cash_income) as cash_income";
			
		String con ="stat_type='day' AND flag_type='%s' AND user_type='%s' group by stat_date";
		String condition = String.format(con, name_type,index);
		//处理类别为总体
		if(index.equals("all"))
		{			
			con ="stat_type='day' AND flag_type='%s' group by stat_date";
			condition = String.format(con, name_type);			
		}

		list = resumeIncomeService.getListByConditon(select, condition, null);	
		
		TreeMap<String, List<ResumeIncome>> stat_dateMap = new TreeMap<String, List<ResumeIncome>>(ReflectUtil.groupByField(list,"stat_date"));
		
		Multimap<String, Object> dataMap= ArrayListMultimap.create();		
		List<Object> dateList = Lists.newArrayList();
	
		String[] Array={"总收入","现金收入"};
		
		for (String date : stat_dateMap.keySet()) {
			
			dateList.add(date);
			List<ResumeIncome> dayList = stat_dateMap.get(date);
		
			for (String  c : Array) {	
		
				ResumeIncome vo = dayList.get(0);		
				
				int sum_income=Integer.parseInt(ReflectUtil.getVal(vo,"sum_income").toString());
				int cash_income=Integer.parseInt(ReflectUtil.getVal(vo,"cash_income").toString());
				
				Object value;
				
				if(c.equals("总收入"))//总收入实际是现金收入，现金收入实际是差额。。为了放入js里
				{
					value=cash_income;
				}
				else
				{
					value=sum_income-cash_income;
				}
				
				dataMap.put(c, value);
				
			}
		}
		
		JSONObject jsonObject = new JSONObject(); 	
		jsonObject.put("time", dateList);
		jsonObject.put("cate", Array);
		jsonObject.putAll(dataMap.asMap());
		return jsonObject.toString();		
		
	}	
	
	
	 /**
	 * 简历数量供应--和弦图
	 * @param city 
	 * @param cate
	 * @param dateGroup
	 * @param beginDate
	 * @return
	 */
	public static String getResumeQuantityJson(String city,String cate,String dateGroup,String beginDate){
		
		if (city == null||cate == null) {
			return Collections.emptyList().toString();
		}
		
		List<ResumeQuantity> list=Lists.newArrayList();
		
		String stat_type="";
		if(dateGroup.equals("stat_date"))
			stat_type="day";
		else stat_type="week";
		
		String sel = " %s,city_name,cate_name,resume_city_count,supply_position,position_que,resume_que";
		String select = String.format(sel,dateGroup);
	
		String con ="cate_name='%s' AND city_name='%s' AND stat_type ='%s' AND  %s ='%s'";
		String condition = String.format(con,cate,city,stat_type,dateGroup,beginDate);
		list = resumeQuantityService.getListByConditon(select, condition, null);		
				
		TreeMap<String, List<ResumeQuantity>> stat_dateMap = new TreeMap<String, List<ResumeQuantity>>(ReflectUtil.groupByField(list,dateGroup));
		
		//城市对应简历数     包括两种缺口  只出现一种        城市：简历数， 缺口：简历数
		HashMap<Object, Integer> city_resumeMap = new HashMap<Object, Integer>();
		
		for (String date : stat_dateMap.keySet()) {
			List<ResumeQuantity> dayList = stat_dateMap.get(date);
			ResumeQuantity vo = dayList.get(0);
			
			String x =  vo.getResume_city_count();			
			String temp[]=x.split(";");
			
			for(String s:temp)
			{				
				String ss[]=s.split(",");	
				city_resumeMap.put(ss[0], Integer.parseInt(ss[1]));
			}			
			if(vo.getResume_que()>0)
			{
				city_resumeMap.put("缺口", vo.getResume_que());					
			}else
			{
				city_resumeMap.put("缺口", vo.getPosition_que());
			}	
		}	
		
		List<Object> nodes_Array = new ArrayList<Object>();
		List<Object> links_Array = new ArrayList<Object>();
		//权值
		int weight = 0;
		
		for (Object city1 : city_resumeMap.keySet()) {
			
			//nodes
			JSONObject item = new JSONObject();				
			item.put("name", city1);
			nodes_Array.add(item);	
			
			weight+=city_resumeMap.get(city1);
		}
		weight/=28;
		
		for (Object city1 : city_resumeMap.keySet()) {
			
			//links      {source: '北京销售', target: '北京', weight: 63, name: '职位'},	{source: '北京销售', target: '缺口', weight: 63, name: '职位'}		
			JSONObject item1 = new JSONObject();
			String t=city_resumeMap.get(city1).toString();
			t="->简历："+t+"->";
			
			item1.put("source", city+cate);
			item1.put("target", city1);
			item1.put("weight", weight);
			item1.put("name", t);			
			links_Array.add(item1);
			
			//  {target: '北京销售', source: '北京', weight: 890},	{target: '北京销售', source: '缺口', weight: 368}
			JSONObject item2 = new JSONObject();
			item2.put("target", city+cate);
			item2.put("source", city1);			
			item2.put("weight", city_resumeMap.get(city1));
			links_Array.add(item2);
		}
	
		JSONObject item = new JSONObject();		
		item.put("name", city+cate);
		nodes_Array.add(item);	

		String[] legend_Array={city+cate};
			
		JSONObject jsonObject = new JSONObject();
		
		jsonObject.put("nodes_Array", nodes_Array);
		jsonObject.put("legend", legend_Array);	
		jsonObject.put("links_Array", links_Array);
		jsonObject.put("beginDate", beginDate);	
		return jsonObject.toString();
	}	
	
	 /**
	 * 职位热搜词&简历亮点词--河流图
	 * @param city 
	 * @param cate
	 * @param beginDate
	 * @return
	 */
	public static String getResumeEntWordJson(String city,String cate,String beginDate){
	
		if (city == null||cate == null||beginDate == null) {
			return Collections.emptyList().toString();
		}
	/*	
		//先是通过week_begin   week 查到top10	
		String select = " week_begin,city_name,cate_name,word_count";
		String con ="cate_name='%s' AND city_name='%s' AND stat_type ='week' AND  week_begin ='%s'";
		String condition = String.format(con,cate,city,beginDate);
		List<ResumeEntWord> list=Lists.newArrayList();
		list = resumeEntWordService.getListByConditon(select, condition, null);		
			
		//本周搜索词	
		List<String> week_words = new ArrayList<String>();		
		int count=0;		
		ResumeEntWord vo1 = list.get(0);			
		String xx =  vo1.getWord_count();			
		String temps[]=xx.split(";");
			
		for(String s:temps)
		{						
			if(count==10)break; //top10
			String ss[]=s.split(",");	
			week_words.add(ss[0]);				
			count++;			
		}			
	
		//本周搜索词	
//		List<String> week_words = new ArrayList<String>();
//		week_words.add("周末兼职");
//		week_words.add("兼职");
//		week_words.add("兼职招聘");
//		week_words.add("临时工日结");
//		week_words.add("日结工资工作");
//		week_words.add("暑假工");
//		week_words.add("在家兼职");
//		week_words.add("小时工");
//		week_words.add("日结");
//		week_words.add("晚上兼职招聘");
		
		
		//7天数据中  通过top10 分别找到每天的搜索量
		
//		List<ResumeEntWord> list=Lists.newArrayList();
	
		//搜索词   对应 时间+次数
		Multimap<String, String> wordMap= ArrayListMultimap.create();	
		
		String select1 = " stat_date,city_name,cate_name,word_count";	
		String con1 ="cate_name='%s' AND city_name='%s' AND stat_type ='day' AND week_begin = '%s' and  stat_date >='%s' and stat_date <=date_sub('%s',interval -6 day)";
		String condition1 = String.format(con1,cate,city,beginDate,beginDate,beginDate);
		list = resumeEntWordService.getListByConditon(select1, condition1, null);		
				
		TreeMap<String, List<ResumeEntWord>> stat_dateMap = new TreeMap<String, List<ResumeEntWord>>(ReflectUtil.groupByField(list,"stat_date"));
		
		for (String date : stat_dateMap.keySet()) {
			
			//还是用map存好进行匹配
			HashMap<Object, Integer> word = new HashMap<Object, Integer>();		
			List<ResumeEntWord> dayList = stat_dateMap.get(date);
			ResumeEntWord vo = dayList.get(0);
			
			String x =  vo.getWord_count();			
			String temp[]=x.split(";");			
			for(String s:temp)
			{				
				String ss[]=s.split(",");
				word.put(ss[0],Integer.parseInt(ss[1]));				
			}
			//如果top10在这天里存在， 放入时间+次数，若没有，放入时间+0 
			for(String c:week_words)
			{
				if(word.containsKey(c))
				{
					wordMap.put(c,date+";"+word.get(c));					
				}
				else wordMap.put(c,date+";0");				
			}
		}
		
		//拼json	
		List<Object> json_datalist = new ArrayList<Object>();
		
		for(String w : wordMap.keySet())
		{					
			JSONObject json_data = new JSONObject();
			json_data.put("name",w);
			json_data.put("weight",123);
						
			List<Object> json_evolution = new ArrayList<Object>();			
			for(String temp : wordMap.get(w))
			{
				JSONObject json2 = new JSONObject();				
				json2.put("time",temp.split(";")[0]);
				json2.put("value",Integer.parseInt(temp.split(";")[1])/10);
				
				JSONObject detail_json = new JSONObject();
				detail_json.put("text",temp.split(";")[1]);
				detail_json.put("link","");
				
				json2.put("detail",detail_json);				
				json_evolution.add(json2);				
			}		
			json_data.put("evolution",json_evolution);
			json_datalist.add(json_data);		
		}
	
		JSONObject jsonObject = new JSONObject();	
		jsonObject.put("datalist", json_datalist);	
		return jsonObject.toString();
	*/	
		
		//查两周的数据
		//先是通过week_begin   week 查到top10	
		String select = " week_begin,city_name,cate_name,word_count";
		String con ="cate_name='%s' AND city_name='%s' AND stat_type ='week' AND  (week_begin ='%s' OR week_begin = date_sub('%s',interval -7 day)) ";
		String condition = String.format(con,cate,city,beginDate,beginDate);
		List<ResumeEntWord> list=Lists.newArrayList();
		list = resumeEntWordService.getListByConditon(select, condition, null);		
		
		//拼json	  
		List<Object> datalist = new ArrayList<Object>();
		
		for(ResumeEntWord vo1:list)
		{
			//本周搜索词	
			List<String> week_words = new ArrayList<String>();		
			int count=0;			
			String xx =  vo1.getWord_count();			
			String temps[]=xx.split(";");
			
			for(String s:temps)
			{						
				if(count==10)break; //top10
				String ss[]=s.split(",");	
				week_words.add(ss[0]);				
				count++;			
			}	
			
			//beginDate根据周不同  二有变化
			
			beginDate=vo1.getWeek_begin();
			
			//搜索词   对应 时间+次数
			Multimap<String, String> wordMap= ArrayListMultimap.create();	
			
			String select1 = " stat_date,city_name,cate_name,word_count";	
			String con1 ="cate_name='%s' AND city_name='%s' AND stat_type ='day' AND week_begin = '%s' and  stat_date >='%s' and stat_date <=date_sub('%s',interval -7 day)";
			String condition1 = String.format(con1,cate,city,beginDate,beginDate,beginDate);
			list = resumeEntWordService.getListByConditon(select1, condition1, null);		
					
			TreeMap<String, List<ResumeEntWord>> stat_dateMap = new TreeMap<String, List<ResumeEntWord>>(ReflectUtil.groupByField(list,"stat_date"));
			
			for (String date : stat_dateMap.keySet()) {
				
				//还是用map存好进行匹配
				HashMap<Object, Integer> word = new HashMap<Object, Integer>();		
				List<ResumeEntWord> dayList = stat_dateMap.get(date);
				ResumeEntWord vo = dayList.get(0);
				
				String x =  vo.getWord_count();			
				String temp[]=x.split(";");			
				for(String s:temp)
				{				
					String ss[]=s.split(",");
					word.put(ss[0],Integer.parseInt(ss[1]));				
				}
				//如果top10在这天里存在， 放入时间+次数，若没有，放入时间+0 
				for(String c:week_words)
				{
					if(word.containsKey(c))
					{
						wordMap.put(c,date+";"+word.get(c));					
					}
					else wordMap.put(c,date+";0");				
				}
			}
			
			List<Object> json_datalist = new ArrayList<Object>();
			
			for(String w : wordMap.keySet())
			{					
				JSONObject json_data = new JSONObject();
				json_data.put("name",w);
				json_data.put("weight",123);
							
				List<Object> json_evolution = new ArrayList<Object>();			
				for(String temp : wordMap.get(w))
				{
					JSONObject json2 = new JSONObject();				
					json2.put("time",temp.split(";")[0]);
					json2.put("value",Integer.parseInt(temp.split(";")[1])/10);
					
					JSONObject detail_json = new JSONObject();
					detail_json.put("text",temp.split(";")[1]);
					detail_json.put("link","");
					
					json2.put("detail",detail_json);				
					json_evolution.add(json2);				
				}		
				json_data.put("evolution",json_evolution);
				json_datalist.add(json_data);		
			}
			
			datalist.add(json_datalist);					
		}
			
		JSONObject jsonObject = new JSONObject();	
		jsonObject.put("datalist", datalist);	
		return jsonObject.toString();
	}

	
	 /**
	 * 字符云--
	 * @param city 
	 * @param cate
	 * @param beginDate
	 * @return
	 */
	public static String getResumeZifuyunJson(String city,String cate,String beginDate){
		
		if (city == null||cate == null||beginDate == null) {
			return Collections.emptyList().toString();
		}
				
		List<ResumeEntWord> list=Lists.newArrayList();
				
		String select = " stat_date,city_name,cate_name,word_count";
	
		String con ="cate_name='%s' AND city_name='%s' AND stat_type ='day' AND  stat_date ='%s' ";
		String condition = String.format(con,cate,city,beginDate,beginDate,beginDate);
		list = resumeEntWordService.getListByConditon(select, condition, null);		
				
		TreeMap<String, List<ResumeEntWord>> stat_dateMap = new TreeMap<String, List<ResumeEntWord>>(ReflectUtil.groupByField(list,"stat_date"));
		
		HashMap<Object, Integer> word = new HashMap<Object, Integer>();
		
		for (String date : stat_dateMap.keySet()) {
			
			List<ResumeEntWord> dayList = stat_dateMap.get(date);
			ResumeEntWord vo = dayList.get(0);
			
			String x =  vo.getWord_count();			
			String temp[]=x.split(";");
			
			for(String s:temp)
			{				
				String ss[]=s.split(",");
				word.put(ss[0],Integer.parseInt(ss[1]));				
			}
		}
		
		List<Object> json_datalist = new ArrayList<Object>();
		
		for(Object c: word.keySet())
		{
			JSONObject item = new JSONObject();		
			item.put("name", c.toString());
			item.put("value", word.get(c));
			
			JSONObject json = new JSONObject();	
			json.put("color", "black");
			
			JSONObject json1 = new JSONObject();
			json1.put("normal", json);
			
			item.put("itemStyle", json1);
			
			json_datalist.add(item);	
		}
			
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("datalist", json_datalist);
		jsonObject.put("word", word);
		
		return jsonObject.toString();
	}	

	
	public static String getReadDownData(String dateGroup, String city,
			String cate, String cycle) {
		String condition = 	String.format("city_name='%s' AND cate_name='%s' AND cycle_type='%s'",city,cate,cycle);
		Object[] colNames = {"sum_delivery", "zd_delivery","sum_read", "zd_read","sum_down", "zd_down"};
		String colString = Joiner.on(",").skipNulls().join(ImmutableList.builder().add(dateGroup).add(colNames).build());
		
		List<ResumeReadDown> rsList = resumeService.getListByConditon(colString, condition, dateGroup);
		
		List<Object> days = Lists.newArrayList();
		Multimap<String, Object> dataMap= ArrayListMultimap.create();
		for(ResumeReadDown vo : rsList){
			days.add(ReflectUtil.getVal(vo, dateGroup));
			for(Object col: colNames){
				dataMap.put((String)col, ReflectUtil.getVal(vo, (String)col));
			}
		}
		
		JSONObject obj = new JSONObject();
		obj.put("time", days);
		obj.putAll(dataMap.asMap());
		return obj.toString();
	}

	public static String getQualityRadarData(String dateGroup, String statType, String dateVal,
			String city, String cate, String education, String salary, String workYears) {
		ImmutableMap<String, String> fieldVap = ImmutableMap.of("education", education,"salary",salary,"work_years",workYears);
		String contition = "stat_type='%s' AND %s='%s'AND city_name='%s' AND cate_name='%s' AND %s='%s'";
		
		String cols = "sum(jianli_count) as jianli_count,sum(zhiwei_count) as zhiwei_count";
		List<Object> resumeDataList = Lists.newArrayList();
		List<Object> jobDataList = Lists.newArrayList();
		for(String f: fieldVap.keySet()){
			String con = String.format(contition, statType,dateGroup, dateVal, city, cate, f, fieldVap.get(f));
			List<ResumeQuality> list = resumeQualityService.getListByConditon(cols, con, null);
			ResumeQuality vo = null;
			if(CollectionUtils.isEmpty(list)){
				vo = new ResumeQuality();
			}else{
				vo = list.get(0);
			}
			resumeDataList.add(ReflectUtil.getVal(vo, "jianli_count"));
			jobDataList.add(ReflectUtil.getVal(vo, "zhiwei_count"));
		}
		
		JSONObject obj = new JSONObject();
		obj.put("resumeData",resumeDataList);
		obj.put("jobData",jobDataList);
		return obj.toString();
	}

	public static String getQualityLineData(String dateGroup, String statType, String city,
			String cate, String education, String salary, String workYears) {
		String CON_PTN = "city_name='%s' AND cate_name='%s' AND education='%s' AND salary='%s' AND work_years='%s' AND stat_type='%s' group by %s";
		String cols = dateGroup+", sum(jianli_count) as jianli_count,sum(zhiwei_count) as zhiwei_count";
		String condition = String.format(CON_PTN, city, cate, education, salary, workYears, statType, dateGroup);
		List<ResumeQuality> list = resumeQualityService.getListByConditon(cols, condition, dateGroup);
		
		List<Object> zhiwei_List = Lists.newArrayList();
		List<Object> jianli_List = Lists.newArrayList();
		List<Object> dayList = Lists.newArrayList();
		for(ResumeQuality vo: list){
			dayList.add(ReflectUtil.getVal(vo, dateGroup));
			jianli_List.add(ReflectUtil.getVal(vo, "jianli_count"));
			zhiwei_List.add(ReflectUtil.getVal(vo, "zhiwei_count"));
		}
		
		JSONObject obj = new JSONObject();
		obj.put("resumeData",jianli_List);
		obj.put("jobData",zhiwei_List);
		obj.put("time",dayList);
		
		return obj.toString();
	}

	/**
	 * 人才流入
	 * @author houzuoxin
	 * */
	public static String getTalentInflowData(String dateGroup,String city, String cate, String seldate) {
	   if("".equals(seldate))
	   {
		   seldate=resumeTalentInflowService.getMaxDate();
	   }
	   String condition=" cate_name='"+cate+"' and at_city='"+city+"' and";
		//判断是周，月，日
		if("week".equals(dateGroup))
		{
			condition=condition+" '"+seldate+"'>=week_begin and '"+seldate+"'<=week_end and stat_type='week'";
		}else if("month".equals(dateGroup)){
			String month=seldate.replaceAll("-", "").substring(0, 6);
			condition=condition+" month_num='"+month+"' and stat_type='month'";
		}else {
			condition=condition+" stat_date='"+seldate+"' and stat_type='day'";
		}
		//String valcityData="";
		List<ResumeTalentInflow> cityresult=resumeTalentInflowService.getListByConditon("to_city", condition, null);
		if(cityresult.size()>0)
		{
		String[] tempcitydata=cityresult.get(0).getTo_city().split(";");
		 List<JSONObject> valData = Lists.newArrayList();
		 List<List<JSONObject>> resData = Lists.newArrayList();
		 int maxdata=0;
		for(int i=0;i<tempcitydata.length;i++)
		{   if((!city.equals(tempcitydata[i].split(",")[0]))&&Integer.parseInt(tempcitydata[i].split(",")[1])!=0)
			{
				List<JSONObject> curData = Lists.newArrayList();
				JSONObject json = new JSONObject();
				json.put("name",city);
				JSONObject json3 = new JSONObject();
				int tempdata=Integer.parseInt(tempcitydata[i].split(",")[1]);
				json3.put("name",tempcitydata[i].split(",")[0]);
				json3.put("value",tempdata);
				if(maxdata<tempdata)
					maxdata=tempdata;
				curData.add(json3);
				curData.add(json);
				valData.add(json3);
				resData.add(curData);
				/*tocityData =tocityData+"[{name:"+city+"},{name:"+tempcitydata[i].split(",")[0]+",value:"+tempcitydata[i].split(",")[1]+"}],";
				valcityData=valcityData+"{name:"+tempcitydata[i].split(",")[0]+",value:"+tempcitydata[i].split(",")[1]+"},";
		*/	}
		}
		net.sf.json.JSONObject obj = new net.sf.json.JSONObject();
		obj.put("tocityData",resData);
		obj.put("valcityData",valData);
		obj.put("maxData",maxdata);
		obj.put("selDate",seldate);
		return obj.toString();
		}
		return "{}";
	}
	
	
	
}
